/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.memberView;
/**
 * 说明：
 * 订单详细
 * @Author：黄盛强
 * @Date：2012-03-26
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumnModel;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TMySiteGift;
import com.ruipengkj.bean.TSiteGift;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.jpanel.RepeatImagePanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.DGiftDetailModel;
import com.ruipengkj.service.TMySiteGiftService;
import com.ruipengkj.service.TShopVisitService;
import com.ruipengkj.service.TSiteGiftService;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BaseDialog;

public class MemberDGiftDialog extends BaseDialog {

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent cartIDTF = null; //会员卡号
	private JLabelJTextFieldComponent telTF = null; //手机号
	
	private JLabelJTextFieldComponent name = null;//用户名
	private JLabelJTextFieldComponent point;//积分
	private JButton dGiftButton; //兑换礼品
	private JButton cancelButton; //关闭
	
	private JTable table = null;//订单Table
	private DGiftDetailModel dGiftDetailModel;//model
	private JScrollPane tableJsp;
	private TUserService tUserService;
	private TSiteGiftService tSiteGiftService;
	private TShopVisitService tShopVisitService;
	private TMySiteGiftService tMySiteGiftService;
	private FlowLayout flowLayout;
	private TUser user=new TUser();
	
	public MemberDGiftDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tUserService = BuildSpringApplication.getInstance().getTUserServiceImpl();
		tShopVisitService = BuildSpringApplication.getInstance().getTShopVisitServiceImpl();
		tSiteGiftService = BuildSpringApplication.getInstance().getTSiteGiftServiceImpl();
		tMySiteGiftService = BuildSpringApplication.getInstance().getTMySiteGiftServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
		this.setSize(900, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		
	}
	
	private void init() {
		flowLayout=new FlowLayout(FlowLayout.LEFT);
		
		dGiftDetailModel = new DGiftDetailModel();
		table=uiFactory.createRPJTable(dGiftDetailModel, this, this, 30, JLabel.CENTER);
		tableJsp=uiFactory.createJScrollPane(table);
		TableColumnModel columnModel = table.getColumnModel();
		columnModel.getColumn(0).setCellEditor(table.getDefaultEditor(Boolean.class));
		columnModel.getColumn(0).setCellRenderer(table.getDefaultRenderer(Boolean.class));
//		this.add(this.topPanel(), BorderLayout.NORTH);
//		this.add(tableJsp, BorderLayout.CENTER);
		
		JPanel main_jp=new JPanel(new BorderLayout());
		main_jp.setBackground(Util.PANEL_COLOR);
		main_jp.add(this.topPanel(), BorderLayout.NORTH);
		main_jp.add(tableJsp, BorderLayout.CENTER);
		main_jp.add(this.bottomPanel(), BorderLayout.SOUTH);
		this.add(main_jp);
	}
	
	//头部面板
	private JPanel topPanel(){
		JPanel p1 = new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.DIALOG_TOP_BG_1).getImage());
		p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		name = new JLabelJTextFieldComponent(" 用 户 名：",20);
		cartIDTF = new JLabelJTextFieldComponent("会员卡号：",20);
		telTF = new JLabelJTextFieldComponent("手机号：",20);
		point = new JLabelJTextFieldComponent(" 积 分 :", 20);
		//dGiftButton=new RPJButtonBlue("兑换礼品");
		//cancelButton=new RPJButtonGray("关闭");
		
		name.getText().setEditable(false);
		cartIDTF.getText().setEditable(false);
		telTF.getText().setEditable(false);
		point.getText().setEditable(false);
	
		p1.add(name);
		p1.add(cartIDTF);
		p1.add(telTF);
		p1.add(point);
		//p1.add(dGiftButton);
		//p1.add(cancelButton);
		return p1;
	}
	private JPanel bottomPanel(){
		JPanel p1 = new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.DIALOG_TOP_BG_1).getImage());
		p1.setLayout(new FlowLayout(FlowLayout.CENTER));
		dGiftButton=new RPJButtonBlue("兑换礼品");
		cancelButton=new RPJButtonGray("关闭");
		p1.add(dGiftButton);
		p1.add(cancelButton);
		return p1;
	}
	private void initListener(){
		dGiftButton.addActionListener(this);
		cancelButton.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==dGiftButton){
			//设置会员级别
			if(table.getSelectedRow()!=-1){
					boolean[] bools = dGiftDetailModel.getBools();
					boolean select = false;//检查是否有选中的商品 如果没选中false 否则true
					for (int i = bools.length -1 ; i >= 0; --i) {
						if (bools[i]) {
							select = true;
							break;
						}
					}
					if (!select) {
						JOptionPane.showMessageDialog(null, "请先选择礼品！");
						return;
					} else {
						try {
							int point=0;
							TUser tUser = tUserService.getByIdService(user.getUserID());
							List<TSiteGift> tSiteGiftList = dGiftDetailModel.getTCommodityList();
							TMySiteGift tMySiteGift=new TMySiteGift();
							Date date=new Date();
							for(int i=0;i<tSiteGiftList.size();i++){
								if (bools[i]) {
									point=tSiteGiftList.get(i).getPoint()+point;
								}
							}
							if(point <=tUser.getPoint()){
								for(int i=0;i<tSiteGiftList.size();i++){
									if (bools[i]) {
										tMySiteGift.setGiftCount(1);
										tMySiteGift.setHappenTime(date);
										tMySiteGift.setMySiteGiftID(UUID.GenUUID15());
										tMySiteGift.setSender(ApplicationContext.OPERATOR_ID);
										tMySiteGift.setStatus((byte)1);
										tMySiteGift.setTSiteGift(tSiteGiftList.get(i));
										tMySiteGift.setTUser(tUser);
										tMySiteGiftService.saveService(tMySiteGift);
									}
								}
								int points = tUser.getPoint()-point;
								tUser.setPoint(points);
								tUserService.saveOrUpdateService(tUser);
								JOptionPane.showMessageDialog(null, "兑换礼品成功！");
								this.dispose();
							}else{
								JOptionPane.showMessageDialog(null, "您的积分不足换取礼品！");
								return;
							}
							table.updateUI();
						} catch (Exception e1) {
							e1.printStackTrace();
						}
					}
			}else{
				JOptionPane.showMessageDialog(null, "请先选择行数据");
			}
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	//数据库中的数据放入list（读取数据）
	private Runnable queryBillDetail=new Runnable() {
		@Override
		public void run() {
			try {
				List<TSiteGift> tSiteGiftList = tSiteGiftService.findService(null);
				
				dGiftDetailModel.setSiteGiftList(tSiteGiftList);
				dGiftDetailModel.setRowData(tSiteGiftList);
//				if (billDetailList != null &&  billDetailList.size()!= 0) {
//					int length = billDetailList.size();
//					for (int i = 0; i < length; ++i) {
//						//获取商品 以便查看
//						billDetailList.get(i).setCommodity(tCommodityService.getByIdService(billDetailList.get(i).getCommodityID()));
//						behaviorDetailModel.addBillDetail(billDetailList.get(i));
//					}
//				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	public void showDialog(TUser tUserID) {
		user = tUserID;
		if (tUserID != null) {
			SwingUtilities.invokeLater(queryBillDetail);
			cartIDTF.getText().setText(tUserID.getCardNum());
			telTF.getText().setText(tUserID.getMobile());
			name.getText().setText(tUserID.getUserName());
			point.getText().setText(tUserID.getPoint()+"");
		}
		this.setVisible(true);
	}
}